package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportLqBmjrsyjcjlbBa1;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrsyjcjlbBa1Vo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportLqBmjrsyjcjlbBa2Bo;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrsyjcjlbBa2Vo;
import com.ruoyi.report_lq.domain.ReportLqBmjrsyjcjlbBa2;
import com.ruoyi.report_lq.mapper.ReportLqBmjrsyjcjlbBa2Mapper;
import com.ruoyi.report_lq.service.IReportLqBmjrsyjcjlbBa2Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class ReportLqBmjrsyjcjlbBa2ServiceImpl implements IReportLqBmjrsyjcjlbBa2Service {

    private final ReportLqBmjrsyjcjlbBa2Mapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportLqBmjrsyjcjlbBa2Vo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     *
     * @param infoId
     * @return 薄膜加热集合
     * @method 根据info-id 查询薄膜子表2的list集合数据
     */
    public List<ReportLqBmjrsyjcjlbBa2Vo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportLqBmjrsyjcjlbBa2> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportLqBmjrsyjcjlbBa2Vo> queryPageList(ReportLqBmjrsyjcjlbBa2Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqBmjrsyjcjlbBa2> lqw = buildQueryWrapper(bo);
        Page<ReportLqBmjrsyjcjlbBa2Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportLqBmjrsyjcjlbBa2Vo> queryList(ReportLqBmjrsyjcjlbBa2Bo bo) {
        LambdaQueryWrapper<ReportLqBmjrsyjcjlbBa2> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqBmjrsyjcjlbBa2> buildQueryWrapper(ReportLqBmjrsyjcjlbBa2Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqBmjrsyjcjlbBa2> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportLqBmjrsyjcjlbBa2::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqBmjrsyjcjlbBa2::getYangpbh, bo.getYangpbh());
        lqw.eq(bo.getJiarqysyzrd() != null, ReportLqBmjrsyjcjlbBa2::getJiarqysyzrd, bo.getJiarqysyzrd());
        lqw.eq(bo.getJiarhclwdzrd() != null, ReportLqBmjrsyjcjlbBa2::getJiarhclwdzrd, bo.getJiarhclwdzrd());
        lqw.eq(bo.getShiybmjrhclwzrdb() != null, ReportLqBmjrsyjcjlbBa2::getShiybmjrhclwzrdb, bo.getShiybmjrhclwzrdb());
        lqw.eq(bo.getZhenrdbpjz() != null, ReportLqBmjrsyjcjlbBa2::getZhenrdbpjz, bo.getZhenrdbpjz());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportLqBmjrsyjcjlbBa2Bo bo) {
        ReportLqBmjrsyjcjlbBa2 add = BeanUtil.toBean(bo, ReportLqBmjrsyjcjlbBa2.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportLqBmjrsyjcjlbBa2Bo bo) {
        ReportLqBmjrsyjcjlbBa2 update = BeanUtil.toBean(bo, ReportLqBmjrsyjcjlbBa2.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增/修改薄膜记录子表2
     */
    public Boolean insertOrUpdateByBmDetail2Bo(List<ReportLqBmjrsyjcjlbBa2Vo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportLqBmjrsyjcjlbBa2 detail = new ReportLqBmjrsyjcjlbBa2();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);
            detail.setYangpbh(detailVo.getYangpbh());
            detail.setJiarqysyzrd(detailVo.getJiarqysyzrd());
            detail.setJiarhclwdzrd(detailVo.getJiarhclwdzrd());
            detail.setShiybmjrhclwzrdb(detailVo.getShiybmjrhclwzrdb());
            detail.setZhenrdbpjz(detailVo.getZhenrdbpjz());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqBmjrsyjcjlbBa2 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
